package cn.itrip.auth.service;

import cn.itrip.common.DtoUtil;
import cn.itrip.common.ErrorCode;
import cn.itrip.common.MD5;
import cn.itrip.common.RedisUtils;
import cn.itrip.dto.Dto;
import cn.itrip.mapper.ItripUser.ItripUserMapper;
import cn.itrip.pojo.ItripUser;
import cn.itrip.vo.ItripUserVO;
import cn.itrip.vo.TokenVO;
import com.alibaba.fastjson.JSON;
import com.mysql.jdbc.StringUtils;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.regex.Pattern;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private ItripUserMapper itripUserMapper;

    @Resource
    private EmailService emailService;

    @Resource
    private SmsService smsService;

    @Resource
    private RedisUtils redisUtils;

    @Resource
    private TokenService tokenService;

    /**
     * 邮箱校验
     * @param email
     * @return
     * @throws Exception
     */
    @Override
    public boolean validEmail(String email) throws Exception {
        String regex="^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$"  ;
        return Pattern.compile(regex).matcher(email).find();
    }

    @Override
    public Dto itriptxAddItripUser(ItripUserVO vo) throws Exception {
        //1.判断是否已注册
        ItripUser itripUser=itripUserMapper.getItripUserByUserCode(vo.getUserCode());
        if (itripUser == null) {
            //创建用户对象
            ItripUser user=new ItripUser();
            user.setUserCode(vo.getUserCode());
            user.setUserPassword(MD5.getMd5(vo.getUserPassword(),32));
            if (vo.getUserName()!=null && vo.getUserName()!=""){
                user.setUserName(vo.getUserName());
            }
            user.setUserType(0);
            user.setActivated(0);
            //判断是手机还是邮箱
            if (validEmail(vo.getUserCode())){
                //2.生成激活码,发送邮箱(保存到redis中)
                String md5 = MD5.getMd5(vo.getUserCode(), 16);
                emailService.send(vo.getUserCode(),md5);
            }else{
                //手机短信 测试模板为1
                String md5 = String.valueOf(MD5.getRandomCode());
                smsService.send(vo.getUserCode(),"1",new String[]{md5,"1"});
            }
            itripUserMapper.insertItripUser(user);
            //3.发送成功后,保存数据
             return DtoUtil.returnSuccess();
        }
        return DtoUtil.returnFail("此用户已经注册", ErrorCode.AUTH_USER_ALREADY_EXISTS);
    }

    /**
     * 手机号校验
     * @param userCode
     * @return
     * @throws Exception
     */
    @Override
    public boolean validPhone(String userCode) throws Exception {
        String regex="^1[3578]{1}\\d{9}$";
        return Pattern.compile(regex).matcher(userCode).find();
    }

    @Override
    public boolean ckusr(String name) throws Exception {
        //1.判断是否存在
        ItripUser itripUser=itripUserMapper.getItripUserByUserCode(name);
        if (itripUser!=null){
            return false;
        }
        return true;
    }

    @Override
    public boolean activate(String name) throws Exception {
        //1.判断是否存激活
        ItripUser itripUser=itripUserMapper.getItripUserByActivate(name);
        if (itripUser==null){
            return false;
        }
        return true;
    }

    @Override
    public boolean activateCkusr(String md5user, String code) throws Exception {
        if (code.equals(redisUtils.getValue(md5user))){
            return true;
        }
        return false;
    }

    @Override
    public boolean activateIsNull(String md5user, String code) throws Exception {
        String md5=redisUtils.getValue(md5user);
        if (md5==null || md5=="") {
            return true;
        }
        return false;
    }

    @Override
    public boolean updateActivateByCode(String name) throws Exception {
        Integer integer = itripUserMapper.updateActivateByCode(name);
        if (integer>0){
            return true;
        }
        return false;
    }

    /**
     * 用户登录
     * @param userCode
     * @param password
     * @param userAgent
     * @return
     */
    @Override
    public Dto doLogin(String userCode, String password, String userAgent) throws Exception{
        //1.判断用户是否存在
        ItripUser user = itripUserMapper.getItripUserByUserCode(userCode);
        //2.密码校验
        if (user!=null && user.getUserPassword().equals(MD5.getMd5(password,32))){
            //3.判断用户是否激活
            if(user.getActivated()!=1){
                return DtoUtil.returnFail("用户未激活",ErrorCode.AUTH_AUTHENTICATION_FAILED);
            }
            //4.生成TokenVO对象
            TokenVO tokenVO=tokenService.genToken(userAgent,user);
            return DtoUtil.returnDataSuccess(tokenVO);
        }
        return DtoUtil.returnFail("用户名或密码错误", ErrorCode.AUTH_PARAMETER_ERROR);
    }


    @Override
    public boolean deleteUser(String user) throws Exception {
        Integer aa=itripUserMapper.deleteItripUserByUserCode(user);
        if (aa>0){
            return true;
        }
        return false;
    }
}
